<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <!-- 1.头部 -->
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="showDarwer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 2.表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="creator" label="作者" width="180">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column label="操作">
          <template scope="row">
            <div class="actions">
              <i
                class="el-icon-view"
                @click="showDarwer('preview', row.row.id)"
              ></i>
              <i
                class="el-icon-edit-outline"
                @click="showDarwer('edit', row.row.id)"
              ></i>
              <i
                class="el-icon-delete"
                @click="showDarwer('delete', row.row.id)"
              ></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 3.分页组件 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      v-if="box"
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      ><div>
        <h1>作者:{{ formData.stem }}</h1>
        <p v-html="formData.content"></p></div
    ></el-drawer>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      v-else
    >
      <el-form
        label-width="80px"
        :model="formData"
        :rules="formDataRules"
        ref="formDataRef"
      >
        <el-form-item label="面经标题" prop="stem">
          <el-input
            placeholder="请输入面经标题"
            v-model="formData.stem"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor v-model="formData.content"></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doAdd">确认</el-button>
          <el-button type="danger">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
//导入接口
import {getArticle ,getArticleDetail,ediArticle,deltelArticle} from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
 
import { quillEditor } from 'vue-quill-editor'
//导入新增接口
import {getCreate} from '@/api/article'
export default {
    components: {
    quillEditor
  },
  name: 'article-page',
  data () {
    return {
      current:1,
      pageSize:10,
      list:[],
      total:0,
      drawer:false,
      title:'',
      formData:{
        stem:'',
        content:''
      },
      box:false,
      formDataRules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },
  // 最早发送ajax的钩子
 created () {
this.initData()
  },
  //方法
  methods: {
    // 分页方法
      handleCurrentChange (val) {
      // 处理当前页变化
      this.current = val
      this.initData()
    },
    //右侧弹窗校验搞弹窗的
    doAdd(){
      this.$refs.formDataRef.validate( async (valid)=>{
        if(valid){
       const {data} = getCreate({stem:this.formData.stem,
          content:this.formData.content
          })
          this.$message.success('新增成功')
          setTimeout(()=>{
            this.initData()
          },1000)
          this.formData.stem=''
          this.formData.content=''
          this.drawer=false
        }else{
            this.$message.danger('输入错误')
          }
      })
    },
    // 请求跟新视图
    async initData(){
      const {data} = await getArticle({
      current:this.current,
      pageSize:this.pageSize
    })
      this.list = data.rows
      console.log(this.list);
 this.total = data.total
    },
    // 做盒子的显示隐藏
   async showDarwer(type,id){
      this.drawer = true
      console.log(type);
      //根据type来显示内部的内容
      if(type === 'add'){
        this.title = '新增面经'
        // this.box = false
      }else if(type === 'preview'){
        this.title = '查看面经'
        // 点击查看面经时为真显示编辑页面
        this.box=true
        // 盒子为真就显示内容
       const {data} =  await getArticleDetail(id)
       this.formData.stem = data.stem
       this.formData.content = data.content
      }else if( type === 'delete'){
        // 删除操作
        this.drawer = false
     await deltelArticle(id)
        setTimeout(()=>{
            this.initData()
          },1000)
          this.$message.success('删除成功')
      }
      
      else{
        this.title = '编辑面经'
        // 点击编辑时发送请求数据的ajax 把内容绑定到data中
         const {data} =  await getArticleDetail(id)
       this.formData.stem = data.stem
       this.formData.content = data.content
       // 发送修改后的数据有了确认按钮的ajax所以不用做
    }
  },
   //在盒子之前做些什么
    handleClose(){
      this.box = false
       if(this.box === false){
          this.formData.stem = ''
      this.formData.content = ''
       }
      this.drawer = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
